<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/discovery/withjid.php';
require_once $basedir . '/../../../classes/xrd/childelement.php';
require_once $basedir . '/../../../classes/xrd/hostmeta.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/host.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink.php';
require_once $basedir . '/../../../classes/xrd/hostmeta/templatelink/title.php';
require_once $basedir . '/../../../classes/xrd/lrdd.php';
require_once $basedir . '/../../../classes/xrd/lrdd/subject.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link.php';
require_once $basedir . '/../../../classes/xrd/lrdd/alias.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/atom.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/salmon.php';
require_once $basedir . '/../../../classes/xrd/lrdd/link/magicpublickey.php';
require_once $basedir . '/../../../classes/xrd.php';

$basedir = dirname(__FILE__);
require_once $basedir . '/../../../classes/atom.php';
require_once $basedir . '/../../../classes/atom/feed.php';
require_once $basedir . '/../../../classes/atom/childelement.php';

require_once $basedir . '/../../../classes/atom/feed/id.php';
require_once $basedir . '/../../../classes/atom/feed/title.php';
require_once $basedir . '/../../../classes/atom/feed/subtitle.php';
require_once $basedir . '/../../../classes/atom/feed/updated.php';
require_once $basedir . '/../../../classes/atom/feed/logo.php';
require_once $basedir . '/../../../classes/atom/feed/generator.php';
require_once $basedir . '/../../../classes/atom/feed/link.php';
require_once $basedir . '/../../../classes/atom/feed/link/alternatehtml.php';
require_once $basedir . '/../../../classes/atom/feed/link/hub.php';
require_once $basedir . '/../../../classes/atom/feed/link/self.php';
require_once $basedir . '/../../../classes/atom/feed/link/salmon.php';

require_once $basedir . '/../../../classes/atom/feed/author.php';
require_once $basedir . '/../../../classes/atom/feed/author/uri.php';
require_once $basedir . '/../../../classes/atom/feed/author/name.php';

require_once $basedir . '/../../../classes/activitystream.php';
require_once $basedir . '/../../../classes/activitystream/subject.php';
require_once $basedir . '/../../../classes/activitystream/objecttype.php';
require_once $basedir . '/../../../classes/activitystream/verb.php';

require_once $basedir . '/../../../classes/atom.php';
require_once $basedir . '/../../../classes/atom/childelement.php';
require_once $basedir . '/../../../classes/atom/entry.php';

require_once $basedir . '/../../../classes/atom/entry/id.php';
require_once $basedir . '/../../../classes/atom/entry/content.php';
require_once $basedir . '/../../../classes/atom/entry/title.php';
require_once $basedir . '/../../../classes/atom/entry/updated.php';
require_once $basedir . '/../../../classes/atom/entry/link.php';

require_once $basedir . '/../../../classes/atom/entry/author.php';
require_once $basedir . '/../../../classes/atom/entry/author/uri.php';
require_once $basedir . '/../../../classes/atom/entry/author/name.php';

require_once $basedir . '/../../../classes/activitystream.php';
require_once $basedir . '/../../../classes/activitystream/actor.php';
require_once $basedir . '/../../../classes/activitystream/objecttype.php';
require_once $basedir . '/../../../classes/activitystream/verb.php';

class Discovery_WithJIDTest extends PHPUnit_Framework_TestCase {

    public function testConstructorOfDiscoveryWithJid() {
        $jid = "tuomas@boobies.com";
        $object = new Discovery_WithJID($jid);
        $this->assertEquals("Discovery_WithJID", get_class($object));
    }
    
    public function testSuccessfulDiscoveryWithJid() {
        
        $jid = "acct:tuomas@boobies.com";
        
        $expectedHostMetaXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>boobies.com</hm:Host>
  <Link rel="lrdd" template="http://boobs.com/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
       $expectedLrddXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Subject>acct:tkoski@boobies.com</Subject>
  <Alias>http://boobs.com/tkoski</Alias>
  <Link rel="http://schemas.google.com/g/2010#updates-from" href="http://boobs.com/updates-from/tuomas.atom" type="application/atom+xml"/>
  <Link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://boobs.com/salmon/tuomas"/>
  <Link rel="magic-public-key" href="data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB"/>
</XRD>
';
       
       $expectedAtomFeed = '<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <generatsor uri="http://madebymonsieur.com" version="0.0.1">MiniMe - Life to XML</generatsor>
  <id>http://localhost/index.php?controller=profile&amp;username=koski</id>
  <title>Tuomas Koski\'s microblog feed.</title>
  <subtitle>My Random Thoughts.</subtitle>
  <logo>http://media.buddycloud.com/channel/54x54/buddycloud.com/koski.png</logo>
  <updated>2010-07-29T17:58:36+02:00</updated>
  <author>
    <name>Tuomas Koski</name>
    <uri>http://localhost/index.php?controller=profile&amp;username=koski</uri>
  </author>
  <link rel="alternate" type="text/html" href="http://localhost/index.php?controller=profile&amp;amp;username=koski-this-is-it"/>
  <link rel="hub" href="http://localhost/index.php?controller=hub"/>
  <link rel="http://salmon-protocol.org/ns/salmon-replies" href="http://localhost/index.php?controller=salmon&amp;username=koski"/>
  <link rel="self" type="application/atom+xml" href="http://localhost/index.php?controller=atom&amp;username=koski"/>
  <activity:subject>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>http://localhost/index.php?controller=profile&amp;username=koski</id>
    <title>Tuomas Koski</title>
    <link rel="alternate" type="text/html" href="http://localhost/index.php?controller=profile&amp;amp;username=koski"/>
  </activity:subject>
  <entry>
    <id>mimime:12345689</id>
    <link rel="alternate" type="application/atom+xml" href="http://localhost/index.php?controller=profile&amp;username=koski&amp;id=mimime:12345689"/>
    <title>Wo-hoo!! It works!!</title>
    <content type="text/html">Wo-hoo!! It works!!</content>
    <updated>2010-07-26T06:42:55+02:00</updated>
    <author>
      <uri>http://localhost/index.php?controller=profile&amp;username=koski</uri>
      <name>Tuomas Koski</name>
    </author>
    <activity:actor>
      <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
      <id>http://localhost/index.php?controller=profile&amp;username=koski</id>
      <title>Tuomas Koski</title>
      <link rel="alternate" type="text/html" href="http://localhost/index.php?controller=profile&amp;username=koski"/>
    </activity:actor>
  </entry>
</feed>
';
        
        //$object = new Discovery_WithJID($jid);
        $mock = $this->getMock('Discovery_WithJID', array('fetchURL'), array($jid));
        $mock->debug = false;
        
        $mock->expects($this->at(0))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobies.com/.well-known/host-meta'))
             ->will($this->returnValue($expectedHostMetaXML));
        $mock->expects($this->at(1))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobs.com/xrd?uri=acct%3Atuomas%40boobies.com'))
             ->will($this->returnValue($expectedLrddXML));
        $mock->expects($this->at(2))
             ->method('fetchURL')
             ->with($this->equalTo('http://boobs.com/updates-from/tuomas.atom'))
             ->will($this->returnValue($expectedAtomFeed));
        $mock->discover();
        
        $this->assertEquals("http://localhost/index.php?controller=hub", $mock->getHub()->getHref());
        $this->assertEquals("http://boobs.com/salmon/tuomas", $mock->getSalmon()->getHref());
        $this->assertEquals("http://boobs.com/updates-from/tuomas.atom", $mock->getAtom()->getHref());
        $this->assertEquals("data:application/magic-public-key,RSA.oSdSbJ99WDC0zRUpk41bpI42FarMo-o6JxJKEeKCPSU1SW9kdXdAUPhWu0JVwdF5rDXWijXaOcdZ3utGwk0pmKxsX6MEQg54L4rfIzWZiHz9OUGgDx9R4tXpm38CXOGfpu4Sx2lmeYVxIii32P32EPJHyZN5Zi9Sr_8zSbXYnM8=.AQAB", $mock->getMagicPublicKey()->getHref());
        $this->assertEquals("http://media.buddycloud.com/channel/54x54/buddycloud.com/koski.png", $mock->getFeed()->getLogo()->getLogo());
        $this->assertEquals("http://localhost/index.php?controller=profile&amp;username=koski-this-is-it", $mock->getFeed()->getAlternateHtml()->getHref());
        $this->assertEquals("Tuomas Koski&#039;s microblog feed.", $mock->getFeed()->getTitle()->getTitle());
    }
    
    /**
     * @expectedException Exception 
     */
    public function testDiscoveryWithJidButFailsInMetaDataDomainValidation() {
        
        $jid = "tuomas@boobies.com";
        
        $expectedHostMetaXML = '<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
  <hm:Host>boobs.com</hm:Host>
  <Link rel="lrdd" template="http://boobs.com/xrd?uri={uri}">
    <Title>Resource Descriptor</Title>
  </Link>
</XRD>
';
        
        //$object = new Discovery_WithJID($jid);
        $mock = $this->getMock('Discovery_WithJID', array('fetchURL'), array($jid));
        
        $mock->expects($this->once())
             ->method('fetchURL')
             ->with($this->equalTo('http://boobies.com/.well-known/host-meta'))
             ->will($this->returnValue($expectedHostMetaXML));
        $mock->debug = false;         
        $mock->discover();
    }
}